package com.axun.ckz.mysocketdemo.base

import android.app.AlertDialog
import android.content.Context
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast

import com.axun.ckz.mysocketdemo.R


/**
 * Created by ckz on 2018/4/23.
 */

open class BaseActivity : AppCompatActivity(), IBaseView {

    var context: Context? = null
        private set
    private var dialog: AlertDialog? = null
    private var view: View? = null
    private var tvMsg: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        context = this
        dialog = AlertDialog.Builder(this, R.style.LoadDialog).create()
        dialog!!.setCancelable(false)
        view = LayoutInflater.from(this).inflate(R.layout.dialog_progress, null)
        tvMsg = view!!.findViewById(R.id.tv_msg)
    }

    fun showToast(toast: String) {
        Toast.makeText(context, toast, Toast.LENGTH_SHORT).show()
    }


    override fun showProgress(progress: String) {
        runOnUiThread {
            if (dialog != null && !dialog!!.isShowing) {
                dialog!!.show()
                dialog!!.setContentView(view!!)
                if (!TextUtils.isEmpty(progress)) {
                    tvMsg!!.visibility = View.VISIBLE
                    tvMsg!!.text = progress
                } else {
                    tvMsg!!.visibility = View.GONE
                }
            }
        }
    }

    override fun hideProgress() {
        runOnUiThread {
            if (dialog != null && dialog!!.isShowing) {
                dialog!!.dismiss()
            }
        }
    }

    override fun showUI(json: String) {

    }

    override fun showError(error: String) {
        showToast(error)
    }

    override fun showFailed(msg: String) {
        showToast(msg)
    }

    private fun createLoadingDialog() {

    }

    override fun onDestroy() {
        super.onDestroy()
        if (dialog != null) {
            dialog!!.dismiss()
        }
    }

}
